package com.zxh.blog.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.gyf.immersionbar.ImmersionBar
import com.zxh.blog.R

class UserInfoActivity : AppCompatActivity() {

    // 返回按钮
    private lateinit var mBackBtn: ImageButton

    // 编辑按钮
    private lateinit var mEdit: TextView

    // 头像
    private lateinit var mAvatar: ImageView

    // 昵称
    private lateinit var mNickName: TextView

    // 邮箱
    private lateinit var mEmail: TextView

    // 性别
    private lateinit var mSex: TextView


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_info)

        ImmersionBar.with(this)
            .statusBarColor(R.color.app_back)
            .statusBarDarkFont(true)
            .fitsSystemWindows(true)
            .init()

        // 初始化控件
        mBackBtn = findViewById(R.id.btn_back)
        mEdit = findViewById(R.id.tv_edit)
        mAvatar = findViewById(R.id.avatar)
        mNickName = findViewById(R.id.tv_nickName)
        mEmail = findViewById(R.id.tv_email)
        mSex = findViewById(R.id.tv_sex)

        // 读取用户信息
        val sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE)
        val nickName = sharedPreferences.getString("nickName", "")
        val avatarURL = sharedPreferences.getString("avatar", "")
        var sex = sharedPreferences.getString("sex", "")
        val email = sharedPreferences.getString("email", "")

        // 给控件赋值
        mNickName.text = nickName
        mSex.text = sex
        mEmail.text = email

        // 加载头像
        //imageView关闭硬件加速，降低内存消耗
        mAvatar.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        //降低图片质量
        val options = RequestOptions().format(DecodeFormat.PREFER_RGB_565)
        Glide.with(this)
            .load(avatarURL)
            .apply(options)
            .error(R.drawable.icon_user)
            .into(mAvatar)

        // 返回上一页面
        mBackBtn.setOnClickListener {
            finish()
        }

        // 跳转到编辑页面
        mEdit.setOnClickListener {
            startActivity(Intent(this,EditInfoActivity::class.java))
            finish()
        }
    }
}